//只有患者经纬度，医生没有经纬度有地址的查列表距离（由于分页加载只有10条）医生看患者列表时是一样的
function equallyList(latitude, longitude, that) {
  //latitude   患者纬度
  //longitude   患者经度
  //doctors   没更新时得列表
  //doctorss   每次请求到的新列表
  //doctors[0]doctor.location.distance   
  //that   将this传入这样才能使用setData()
  var QQMapWX = require('qqmap-wx-jssdk.js');
  var demo = new QQMapWX({
    key: 'LXOBZ-4H3CV-YNYPK-UZCTT-UUYU6-QJFO7' // 必填
  });
  //要展示的列表
  let a = that.data.doctors;
  //刷新后的新列表（由于分页加载只有10条）
  let b = that.data.doctorss;
  if (b.length == 0) {
    //添加距离字段
    for (let i = 0; i < a.length; i++) {
      if (!a[i].doctor.distance) {
        a[i].doctor['distance'] = 0;
      } else {
        continue
      }
    }
    console.log(that.data.doctors);
    for (let i = 0, l = a.length; i < l; i++) {
      //对方地址 省 市 所在地名字
      let cc = a[i].doctor.hospitalProvince + a[i].doctor.hospitalCity + a[i].doctor.nmHospital;
      demo.geocoder({
        address: cc,
        success: function(res) {
          let lng = res.result.location.lng;//对方地址经度
          let lat = res.result.location.lat;//对方地址纬度
          let lat1 = latitude;//患者纬度
          let lng1 = longitude;//患者经度
          let lat2 = lat;
          let lng2 = lng;
          console.log(lat1)
          console.log(lng1)
          console.log(lat2)
          console.log(lng2)
          var rad1 = lat1 * Math.PI / 180.0;
          console.log(rad1) 
          var rad2 = lat2 * Math.PI / 180.0;
          console.log(rad2)
          var aaa = rad1 - rad2;
          console.log(aaa)
          var bbb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
          console.log(bbb)
          var r = 6378137;
          //计算出距离单位为米
          var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aaa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bbb / 2), 2)));
          console.log(distance)
          //米与千米的换算
          if (distance > 1000) {
            distance = Math.round(distance / 1000);
          }
          console.log(distance)
          console.log(lng)
          console.log(lat)
          console.log(lat1)
          console.log(lng2)
          let aa = 'doctors[' + i + '].doctor.distance';
          that.setData({
            [aa]: distance,
            waitFor: 1 //使得定时器停止
          })
        }
      });
    }
  } else {
    //添加距离字段
    for (let i = 0; i < b.length; i++) {
      if (!b[i].doctor.distance) {
        b[i].doctor['distance'] = 0;
      } else {
        continue
      }
    }
    for (let i = 0, l = b.length; i < l; i++) {
      let cc = b[i].doctor.hospitalProvince + b[i].doctor.hospitalCity + b[i].doctor.nmHospital;
      demo.geocoder({
        address: cc,
        success: function(res) {
          let lng = res.result.location.lng;
          let lat = res.result.location.lat;
          let lat1 = latitude;
          let lng1 = longitude;
          let lat2 = lat;
          let lng2 = lng;
          var rad1 = lat1 * Math.PI / 180.0;
          var rad2 = lat2 * Math.PI / 180.0;
          var aa = rad1 - rad2;
          var bb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
          var r = 6378137;
          var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bb / 2), 2)));
          if (distance > 1000) {
            distance = Math.round(distance / 1000);
          }
          let bbb = 'doctorss[' + i + '].doctor.distance';
          that.setData({
            [bbb]: distance,
          })
          if (b[9].doctor.distance != 0) {
            let c = [];
            c = a.concat(b);
            that.setData({
              doctors: c,
              waitFor: 1 //使得定时器停止
            })
          }
        }
      });
    }
  }
}
//有患者经纬度,医生有经纬度有地址的查列表距离（由于分页加载只有10条）医生看患者列表时是一样的
function differentList(latitude, longitude, that) {
  
  //that   将this传入这样才能使用setData()
  //要展示的列表
  let a = that.data.doctors;
  //刷新后的新列表（由于分页加载只有10条）
  let b = that.data.doctorss;
  if (b.length == 0) {
    for (let i = 0, l = a.length; i < l; i++) {
      let lng = a[i].doctor.location.longitude;
      let lat = a[i].doctor.location.latitude;
      let lat1 = latitude;
      let lng1 = longitude;
      let lat2 = lat;
      let lng2 = lng;
      var rad1 = lat1 * Math.PI / 180.0;
      var rad2 = lat2 * Math.PI / 180.0;
      var aa = rad1 - rad2;
      var bb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
      var r = 6378137;
      var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bb / 2), 2)));
      if (distance > 1000) {
        distance = Math.round(distance / 1000);
      }
      a[i].doctor.location.distance = distance;
      if (i == 9) {
        that.setData({
          doctor: a
        })
      }
    }
  } else {
    for (let i = 0, l = b.length; i < l; i++) {
      let lng = a[i].doctor.location.longitude;
      let lat = a[i].doctor.location.latitude;
      let lat1 = latitude;
      let lng1 = longitude;
      let lat2 = lat;
      let lng2 = lng;
      var rad1 = lat1 * Math.PI / 180.0;
      var rad2 = lat2 * Math.PI / 180.0;
      var aa = rad1 - rad2;
      var bb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
      var r = 6378137;
      var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bb / 2), 2)));
      if (distance > 1000) {
        distance = Math.round(distance / 1000);
      }
      b[i].doctor.location.distance = distance;
      if (i == 9) {
        let c = [];
        c = a.concat(b);
        that.setData({
          doctors: c
        })
      }
    }
  }
}
//与医生沟通或看医生个人详情时距离(医生个人信息有地址没有经纬度时)
function equallyDetails(latitude, longitude, that) {
 
  //that   将this传入这样才能使用setData()
  let doctors = that.data.doctors;
  var QQMapWX = require('qqmap-wx-jssdk.js');
  var demo = new QQMapWX({
    key: 'LXOBZ-4H3CV-YNYPK-UZCTT-UUYU6-QJFO7' // 必填
  });
  let cc = doctors.hospitalProvince + doctors.hospitalCity + doctors.nmHospital;
  demo.geocoder({
    address: cc,
    success: function(res) {
      let lng = res.result.location.lng;
      let lat = res.result.location.lat;
      let lat1 = latitude;
      let lng1 = longitude;
      let lat2 = lat;
      let lng2 = lng;
      var rad1 = lat1 * Math.PI / 180.0;
      var rad2 = lat2 * Math.PI / 180.0;
      var aa = rad1 - rad2;
      var bb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
      var r = 6378137;
      var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bb / 2), 2)));
      if (distance > 1000) {
        distance = Math.round(distance / 1000);
      }
      doctors.location.distance = distance;
      doctors['distance'] = distance;
      that.setData({
        doctors: doctors
      })
    }
  });
}
//与医生沟通或看医生个人详情时距离(医生个人信息有地址有经纬度时)
function differentDetails(latitude, longitude, that) {
  
  //that   将this传入这样才能使用setData()
  let doctors = that.data.doctors;
  let lng = doctors.location.longitude;
  let lat = doctors.location.latitude;
  let lat1 = latitude;
  let lng1 = longitude;
  let lat2 = lat;
  let lng2 = lng;
  var rad1 = lat1 * Math.PI / 180.0;
  var rad2 = lat2 * Math.PI / 180.0;
  var aa = rad1 - rad2;
  var bb = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var r = 6378137;
  var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(aa / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(bb / 2), 2)));
  if (distance > 1000) {
    distance = Math.round(distance / 1000);
  }
  doctors.location.distance = distance;
  that.setData({
    doctors: doctors
  })
}


module.exports = {
  equallyList: equallyList,
  differentList: differentList,
  equallyDetails: equallyDetails,
  differentDetails: differentDetails
}